package com.gsnd.briuppts.web.controller;

import com.github.pagehelper.PageInfo;
import com.gsnd.briuppts.entity.DO.ManagerFenceHouseExtend;
import com.gsnd.briuppts.entity.ManagerFenceHouse;
import com.gsnd.briuppts.result.Response;
import com.gsnd.briuppts.service.ManagerFhService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/fenceHouse")
public class ManagerFHController {
    @Autowired
    ManagerFhService managerFhService;

    @GetMapping
    public Response<PageInfo<ManagerFenceHouse>> findAllFhByPage(String fhName,
                                    Integer pageNum,
                                    Integer pageSize){
        // 分页查询需要导入额外的jar包，pageHelper
        PageInfo<ManagerFenceHouse> pageInfo = managerFhService.findAllByConditionByPage(fhName, pageNum, pageSize);
        return Response.success(pageInfo);
    }
    @PostMapping("/saveOrUpdate")
    public Response<String> saveOrUpdateFh(@RequestBody ManagerFenceHouse managerFenceHouse){
        managerFhService.addAndEdit(managerFenceHouse);
        return Response.success("cgx:新增/修改成功!");
    }
    @DeleteMapping("/{id}")
    public Response<String> removeById(@PathVariable String id){
//      // 调用Arrays这个数组的工具类，可以将多个参数拼接成list集合
        // 由于这里只有一个参数，idea推荐使用下面个方法
        List<String> list = Collections.singletonList(id);
        managerFhService.removeByIdOrAll(list);
        return Response.success("cgx: 删除单条数据成功");
    }

    @DeleteMapping("/deleteByIdAll")
    public Response<String> removeByIds(@RequestBody List<String> ids){
        managerFhService.removeByIdOrAll(ids);
        return Response.success("cgx: 批量删除成功");
    }

    @GetMapping("/{fhId}")
    public Response<ManagerFenceHouseExtend> findDetailByFhId(@PathVariable String fhId){
        ManagerFenceHouseExtend detailByFhId = managerFhService.findDetailByFhId(fhId);
        return Response.success(detailByFhId);
    }
}
